package com.live.dengdengtv.net;

import android.util.Log;

import com.live.dengdengtv.utils.DianboSignUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zhy.http.okhttp.request.RequestCall;

import java.util.TreeMap;

import okhttp3.Call;

/**
 * Created by wyf on 2016/10/6.
 */
public class DianboNetTask {

    private static final String TAG = "DianboNetTask";


//    public static final String secretId = "AKIDzseMyQzrAYnRxJdqMuhTk0fIGazJk2Rj";
//    public static final String secretkey = "WvFhKdsx9Vk8MSPCaxNoDfx52ueQ7kN1";

    public static final String secretId = "AKIDDnJ7qcipFdyHUWITGydxLdyGRVNDt0um";
    public static final String secretkey = "662oqkW8c2IPLAzlLnS1RgkpnUmvvkOu";


    public static final String METHOD = "GET";
    public static final String HOST = "vod.api.qcloud.com";

    public static final String PATH = "/v2/index.php";
    private static final String PROTOCOL = "https";


//    private static final String url = "https://vod.api.qcloud.com/v2/index.php";
    private static final String URL = PROTOCOL +"://" + HOST + PATH;


    public static void getRecordInfo(String vid, Callback callback){
        TreeMap<String, String> params = genFixParamMap();
        params.put("Action", "DescribeRecordPlayInfo");//接口指令的名称
        params.put("vid", vid);//
//        params.put("notifyUrl", "");//回调地址

        String signature = DianboSignUtils.getSignStr(params);
        params.put("Signature", signature);//签名

        Log.e(TAG,params.toString()+"参数");

        RequestCall requestCall = OkHttpUtils.get()
                .url(URL)
                .params(params)
                .build();

        requestCall.execute(callback);

    }

    /**获取视频所有分类列表*/
    public static void getAllClassList(Callback callback){
        getCommonParamsData("DescribeAllClass", callback);
    }
    /**获取视频分类列表*/
    public static void getCategoryList(Callback callback){
        getCommonParamsData("DescribeClass", callback);
    }

    public static void getVideoInfoById(String fileId, Callback callback){
        TreeMap<String, String> params = genFixParamMap();
        params.put("Action", "DescribeVodPlayUrls");//接口指令的名称
        params.put("fileId", fileId);//文件id Y 示例:"14651978969313585028"

        String signature = DianboSignUtils.getSignStr(params);
        params.put("Signature", signature);//签名

        Log.e(TAG,params.toString()+"参数");

        RequestCall requestCall = OkHttpUtils.get()
                .url(URL)
                .params(params)
                .build();

        requestCall.execute(callback);

    }
    public static void getVideoInfoByName(Callback callback){
        TreeMap<String, String> params = genFixParamMap();
        params.put("Action", "DescribeVodPlayInfo");//接口指令的名称
        params.put("fileName", "烟花瀑布");//文件名 Y
//        params.put("pageNo", "烟花瀑布");//页号 N
//        params.put("pageSize", "烟花瀑布");//分页大小 N
        String signature = DianboSignUtils.getSignStr(params);
        params.put("Signature", signature);//签名

        Log.e(TAG,params.toString()+"参数");

        RequestCall requestCall = OkHttpUtils.get()
                .url(URL)
                .params(params)
                .build();

        requestCall.execute(callback);

    }
    public static void getSerchVideo(String classId, Callback callback){
        TreeMap<String, String> params = genFixParamMap();
        params.put("Action", "DescribeVodInfo");//接口指令的名称
        params.put("classId", classId);//视频分类ID  例:26155
//        params.put("status", "2");//视频状态   2正常
        String signature = DianboSignUtils.getSignStr(params);
        params.put("Signature", signature);//签名

        Log.e(TAG,params.toString()+"参数");

        RequestCall requestCall = OkHttpUtils.get()
                .url(URL)
                .params(params)
                .build();

        requestCall.execute(callback);

    }
    public static void getCommonParamsData(String action, Callback callback){
        TreeMap<String, String> params = genFixParamMap();
        params.put("Action", action);//接口指令的名称
        String signature = DianboSignUtils.getSignStr(params);
        params.put("Signature", signature);//签名

        Log.e(TAG,params.toString()+"参数");

        RequestCall requestCall = OkHttpUtils.get()
                .url(URL)
                .params(params)
                .build();

        requestCall.execute(callback);

    }


    /**公共参数*/
    private static TreeMap<String, String> genFixParamMap() {
        TreeMap<String, String> keyValues = new TreeMap<String, String>();

//        keyValues.put("Action", "DescribeInstances");//接口指令的名称

        /*
        * 区域参数，用来标识希望操作哪个区域的实例。可选:
        * bj:北京, gz:广州, sh:上海, hk:香港, ca:北美
        * */
        keyValues.put("Region", "gz");
        keyValues.put("Timestamp", System.currentTimeMillis()/1000 + "");//当前UNIX时间戳
        String nonce = (int)(Math.random()*100000) + "";
        keyValues.put("Nonce", nonce);//随机正整数，与 Timestamp 联合起来, 用于防止重放攻击

//        keyValues.put("Region", "gz");
//        keyValues.put("Timestamp", "1475782213");//当前UNIX时间戳
//        keyValues.put("Nonce", "578042233");//随机正整数，与 Timestamp 联合起来, 用于防止重放攻击


        //由腾讯云平台上申请的标识身份的 SecretId 和 SecretKey, 其中 SecretKey 会用来生成 Signature
        keyValues.put("SecretId", secretId);
//        keyValues.put("Signature", "");//请求签名，用来验证此次请求的合法性,
        return keyValues;
    }

}
